﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace CSI300Cons.CapitalDownloader
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private static List<PriceRecord> GetYesterClosePrice(List<KeyValuePair<int, string>> NameList)
        {
            List<PriceRecord> recordList = new List<PriceRecord>();

            StringBuilder results = new StringBuilder();

            for (int j = 0; j < 3; j++)
            {
                StringBuilder url = new StringBuilder("http://hq.sinajs.cn/list=");

                for (int i = 0; i < 100; i++)
                {
                    KeyValuePair<int, string> stock = NameList[j * 100 + i];

                    if (stock.Key < 600000)
                    {
                        url.AppendFormat("{0}{1}", "sz", stock.Key.ToString("000000"));
                    }
                    else
                    {
                        url.AppendFormat("{0}{1}", "sh", stock.Key.ToString("000000"));
                    }
                    url.Append(",");
                }

                using (WebClient wc = new WebClient())
                {
                    using (Stream resStream = wc.OpenRead(url.ToString()))
                    {
                        StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
                        results.Append(sr.ReadToEnd());
                    }
                }
            }

            string[] resultList = results.ToString().Split(';');

            PriceRecord record;
            char[] sp = { '0', '6' };
            foreach (string r in resultList)
            {
                if (r.Length < 30)
                    continue;
                record = new PriceRecord();
                record.StockID = Convert.ToInt32(r.Substring(r.IndexOfAny(sp), 6));
                int start = r.IndexOf('"');
                record.StockName = r.Substring(start + 1, r.IndexOf(',') - start - 1);

                start = r.IndexOf(',', r.IndexOf(',') + 1);

                record.CurrentPrice = Convert.ToDouble(r.Substring(start + 1, r.IndexOf(',', start + 1) - start - 1));

                if (record.CurrentPrice == 0)
                {
                    start = r.IndexOf(',', r.IndexOf(',') + 1);
                    record.CurrentPrice = Convert.ToDouble(r.Substring(start + 1, r.IndexOf(',', start + 1) - start - 1));
                }

                record.DownloadDate = DateTime.Today.Date;

                recordList.Add(record);
            }

            return recordList;
        }

        private void btnDo_Click(object sender, EventArgs e)
        {
            var names = DownloaderTask.LoadNameList();

            var prices = GetYesterClosePrice(names);

            StringBuilder sb = new StringBuilder();

            prices.ForEach(p => sb.AppendLine(string.Format("{0} {1} {2}", p.StockID.ToString("000000"), p.StockName, p.CurrentPrice)));

            this.tbResult.Text = sb.ToString();
        }

        private void btnDo2_Click(object sender, EventArgs e)
        {
            var names = DownloaderTask.LoadNameList();
            var list = DownloaderTask.GetCurrentPrice(names);
            
            DownloaderTask.GetCurrentCapital(list, false);

            StringBuilder sb=new StringBuilder();
            list.ForEach(p => sb.AppendLine(string.Format(string.Format("{0}{1}  总股本：{3} 流通股本：{5}", p.StockID.ToString("000000"), p.StockName, p.CurrentPrice, p.TotalCapital, p.TotalValue, p.CurrentCapital))));

            this.tbResult.Text = sb.ToString();
        }
    }
}
